$(function () {
    //自购支付方式
    var zgzjjhZffsCombobox = $("#zjjhglForm :text[name='zgzjjh.zffs.id']").combobox();
    //=======================================
    //=================资金计划列===============
    //=======================================
    //自购资金计划列
    var zgzjjhmxCol = [
        [
            {field: 'checked', checkbox: true},
            {title: '付款日期', field: 'fkrq', editor: {type: 'datebox', options: {required: true}}, width: 150},
            {title: '付款金额(元)', field: 'fkje', editor: {type: 'numberbox', options: {required: true, precision: 2, min: 0,groupSeparator:','}},
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                } ,width: 150}
        ]
    ];
    //统配资金计划列
    var tpzjjhmxCol = [
        [
            {title: '品名', field: 'pm.name', width: 150},
            {title: '核定数量', field: 'hdsl', width: 150},
            {title: '付款日期', field: 'fkrq', editor: {type: 'datebox', options: {required: true}}, width: 150},
            {title: '付款金额(元)', field: 'fkje', editor: {type: 'numberbox', options: {required: true, precision: 2, min: 0,groupSeparator:','}},
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                } ,width: 150}
        ]
    ];
    //自购物资计划
    var zjjhglZgwzjhOptions = {
        type: "Zgwzjh",
        ssgsId: $("#zjjhglForm [name='zgzjjh.sbgs.id']").val(),
        zt: "ysp",
        onChange: function (newValue, oldValue) {
            var g = $(this).combogrid('grid');
            var result = g.datagrid('getSelected');
            $("#zjjhglForm [name='zgzjjh.gys.id']").val(result.gys_id);
            $("#zjjhglForm [name='zgzjjh.gys.qc']").val(result.gys_qc);
            $("#zjjhglForm [name='zgzjjh.kh.id']").val(result.kh_id);
            $("#zjjhglForm [name='zgzjjh.kh.qc']").val(result.kh_qc);
            $("#zjjhglForm [name='zgzjjh.dbf.id']").val(result.dbf_id);
            $("#zjjhglForm [name='zgzjjh.dbf.qc']").val(result.dbf_qc);
            $("#zjjhglForm [name='zgzjjh.lj.id']").val(result.lj_id);
            $("#zgzjjh_li_mc").text(result.lj_mc);

            if(result.lrbl){
                $("#zgzjjh_mll").numberbox('setValue',result.lrbl);
            }
            if (result.xy_fkfs_id) {
                $(zgzjjhZffsCombobox).combobox("setValue", result.xy_fkfs_id);
            }
            $("#zgzjjh_cggs_qc").val(result.cggs_jc);
            $("[name='zgzjjh.cggs.id']").val(result.cggs_id);
        },    //自购资金计划如果非三方类型客户不出现
        onSelect:function(rowIndex,rowData){
            if(rowData.rzlx){
                $("#zgzjjh_kh_th").show();
                $("#zgzjjh_kh_td").show();
            } else{
                $("#zgzjjh_kh_th").hide();
                $("#zgzjjh_kh_td").hide();
            }
        }
    };
    //统配物资计划
    var zjjhglTpwzjhOptions = {
        type: "Tpwzjh",
        ssgsId: $("#zjjhglForm [name='tpzjjh.sbgs.id']").val(),
        zt: "ysp",
        onChange: function (newValue, oldValue) {
            var options = {
                toolbar: '#zjjhgl_zjjhmx_toolbar',
                singleSelect: true,
                fitColumns: true,
                fit: true,
                rownumbers: true,
                showFooter: true,
                onSelect: function (rowIndex, rowData) {
                    datagridOnSelectHandle(rowIndex, rowData);
                }
            };
            var g = $(this).combogrid('grid');
            var result = g.datagrid('getSelected');
            $("#zjjhglForm [name='tpzjjh.gys.id']").val(result.gys_id);
            $("#zjjhglForm [name='tpzjjh.gys.qc']").val(result.gys_qc);
            $("#zjjhglForm [name='tpzjjh.lj.id']").val(result.lj_id);
            $("#tpzjjh_lj_mc").text(result.lj_mc);
//            if (result.sbjg_id) { //申报机构
//                $(tpzjjhSbjgCombotree).combotree("setValue", result.sbjg_id);
//            }
            if (result.xy_fkfs_id) {
                $(tpzjjhZffzCombobox).combobox("setValue", result.xy_fkfs_id);
            }
            $.extend(options, {
                columns: tpzjjhmxCol,
                url: $.webPath() + '/jhgl/zjjhgl/initTpzjjhmx?id=' + newValue
            });
            grid = $('#zjjhgl_zjjhmx_edatagrid').edatagrid(options);
        }
    };
    //资金计划表格
    var grid = null;
    //===========================
    //========资金计划主信息页面=======
    //===========================
    //========================================
    //===============自购资金主信息===============
    //========================================
    //资金计划类型控制表单显示
    $("#zjjhglForm [name='type']").click(function () {
        var options = {
            toolbar: '#zjjhgl_zjjhmx_toolbar',
            fitColumns: true,
            fit: true,
            singleSelect: true,
            checkbox: true,
            checkOnSelect: false,
            selectOnCheck: false,
            rownumbers: true,
            showFooter: true,
            onSelect: function (rowIndex, rowData) {
                datagridOnSelectHandle(rowIndex, rowData);
            }
        };
        if ($(this).val() == "Zgzjjh") {
            $("#zjjhglForm tr[name='zgzjjh']").show();
            $("#zjjhglForm tr[name='tpzjjh']").hide();
            $("#zjjhgl_zjjhmx_toolbar [name='xzzjmx']").show();
            $("#zjjhgl_zjjhmx_toolbar [name='sczjmx']").show();
            addZgzjjhValidate(true);
            addTpzjjhValidate(false);
            //资金计划明细编辑列表
            $.extend(options, {columns: zgzjjhmxCol});
            //修改加载from数据和资金计划明细数据
            if ($("#zjjhgl_content_zjjh_id").val() != "") {
                $.extend(options, {url: $.webPath() + '/jhgl/zjjhgl/zjjhmxList?type=Zgzjjh&id=' + $("#zjjhgl_content_zjjh_id").val()});
            } else {
                $.extend(options, {data: {
                    rows: [
//                        {fkrq: "", fkje: 0},
//                        {fkrq: "", fkje: 0},
                        {fkrq: "", fkje: 0}
                    ],
                    footer: [
                        {fkrq: "合计", fkje: 0}
                    ]}
                });
            }
            grid = $('#zjjhgl_zjjhmx_edatagrid').edatagrid(options);
            $("#zjjhgl_zjjhmx_toolbar").show();
        }
        if ($(this).val() == "Tpzjjh") {
            $("#zjjhglForm tr[name='zgzjjh']").hide();
            $("#zjjhglForm tr[name='tpzjjh']").show();
            $("#zjjhgl_zjjhmx_toolbar [name='xzzjmx']").hide();
            $("#zjjhgl_zjjhmx_toolbar [name='sczjmx']").hide();
            addZgzjjhValidate(false);
            addTpzjjhValidate(true);
            //资金计划明细编辑列表
            $.extend(options, {toolbar:null,columns: tpzjjhmxCol});
            if ($("#zjjhgl_content_zjjh_id").val() != "") {
                $.extend(options, {url: $.webPath() + '/jhgl/zjjhgl/zjjhmxList?type=Tpzjjh&id=' + $("#zjjhgl_content_zjjh_id").val()});
            }
            $.extend(options, {
                data: { rows: [],
                    footer: [
                        {fkrq: "合计", fkje: 0}
                    ]}
            });
            grid = $('#zjjhgl_zjjhmx_edatagrid').edatagrid(options);
            $("#zjjhgl_zjjhmx_toolbar").hide();
        }
    });

    //自购物资计划选择列表
    var zjjhglZgwzjhCombogrid = $("#zjjhglForm :text[name='zgzjjh.zgwzjh.id']").combogrid(zjjhglZgwzjhOptions);

    //资金计划需求时间
    var zgzjjhJhnf = $("#zjjhglForm :text[name='zgzjjh.jhnf']").numberspinner({min: 2000, max: 2100});
    var zgzjjhJhyf = $("#zjjhglForm :text[name='zgzjjh.jhyf']").numberspinner({min: 1, max: 12});

    //自购资金计划申报机构
    //var zgzjjhSbjgCombotree = $("#zjjhglForm :text[name='zgzjjh.sbjg.id']").combotree();

    //自购资金计划申报公司对申报部门的级联操作
//    $("[name='zgzjjh.sbgs.id']").jgbmtree($("[name='zgzjjh.sbbm.id']"), undefined,$("#zgzjjh_sbbm").val(),undefined);
//    readOnly($("[name='zgzjjh.sbgs.id']"));


    //毛利率
    var zgzjjhMllNumberbox = $("#zjjhglForm :text[name='zgzjjh.mll']").numberbox({precision: 4, min: 0, max: 100, decimalSeparator: '.'});

    //自购资金统筹部门
//    var zgzjjhZjtcbmCombotree = $("#zjjhglForm :text[name='zgzjjh.zjtcbm.id']").combotree();

    //自购资金计划添加表单验证
    function addZgzjjhValidate(required) {
        $(zjjhglZgwzjhCombogrid).wzjhForZjjhCombogrid($.extend(zjjhglZgwzjhOptions, {required: required}));
        $(zgzjjhJhnf).numberspinner({min: 2000, max: 2100, required: required, missingMessage: "该输入项为必填项"});
        $(zgzjjhJhyf).numberspinner({min: 1, max: 12, required: required, missingMessage: "该输入项为必填项"});
        //$(zgzjjhSbjgCombotree).jygsjgcombotreechild($("#zjjhglForm [name='zgzjjh.sbgs.id']").val(), {required: required});
//        $(zgzjjhZjtcbmCombotree).jygsjgcombotreechild($("#zjjhglForm [name='zgzjjh.zjtcbm.id']").val(), {required: required});
        $(zgzjjhZffsCombobox).sjzdcombobox("fkfs", {required: required});
    }

    //========================================
    //===============统配资金计划=================
    //========================================
    //统配物资计划选择列表
    var tpwzjhCombogrid = $("#zjjhglForm :text[name='tpzjjh.tpwzjh.id']").combogrid(zjjhglTpwzjhOptions);

    //资金计划需求时间
    var tpzjjhJhnf = $("#zjjhglForm :text[name='tpzjjh.jhnf']").numberspinner({min: 2000, max: 2100});
    var tpzjjhJhyf = $("#zjjhglForm :text[name='tpzjjh.jhyf']").numberspinner({min: 1, max: 12});

    //统配资金计划申报机构
    //var tpzjjhSbjgCombotree = $("#zjjhglForm :text[name='tpzjjh.sbjg.id']").combotree();

    //统配资金计划申报公司对申报部门的级联操作
    $("[name='tpzjjh.sbgs.id']").jgbmtree($("[name='tpzjjh.sbbm.id']"), undefined,$("#tpzjjh_sbbm").val(),'');

    //统配支付方式
    var tpzjjhZffzCombobox = $("#zjjhglForm :text[name='tpzjjh.zffs.id']").combobox();

    //默认选中自购资金计划类型
    var checkedRadio = $("#zjjhglForm [name='type']:checked");
    if (checkedRadio.length == 0)
        $("#zjjhglForm [name='type']:first").click();
    else {
        $(checkedRadio).click();
        $("#zjjhglForm [name='type']").attr("disabled", "true");
    }


    //统配资金计划添加表单验证
    function addTpzjjhValidate(required) {
        $(tpzjjhJhnf).numberspinner({min: 2000, max: 2100, required: required, missingMessage: "该输入项为必填项"});
        $(tpzjjhJhyf).numberspinner({min: 1, max: 12, required: required, missingMessage: "该输入项为必填项"});
        $(tpwzjhCombogrid).wzjhForZjjhCombogrid($.extend(zjjhglTpwzjhOptions, {required: required}));
        //$(tpzjjhSbjgCombotree).jygsjgcombotreechild($("#zjjhglForm [name='tpzjjh.sbgs.id']").val(), {required: required});
        $(tpzjjhZffzCombobox).sjzdcombobox("fkfs", {required: required});
    }

    //===========================
    //========物资计划明细页面========
    //===========================
    //新增物资计划明细
    $("#zjjhgl_zjjhmx_toolbar [name='xzzjmx']").click(function () {
        $(grid).edatagrid('appendRow', {fkrq: "", fkje: 0});
    });
    //删除物资计划明细
    $("#zjjhgl_zjjhmx_toolbar [name='sczjmx']").click(function () {
        var rows = $(grid).edatagrid("getChecked");
        $.each(rows, function (index, value) {
            var index = $(grid).edatagrid('getRowIndex', value);
            $(grid).edatagrid('deleteRow', index);
        });
        countSum();
//        $(grid).edatagrid('destroyRow');
        $(grid).edatagrid('resize');
    });

    //表格处在编辑状态的时候，给表单绑定事件
    function datagridOnSelectHandle(rowIndex, rowData) {
        //获取每个单元格
        $(grid).edatagrid('editRow', rowIndex);
        var editors = $(grid).datagrid('getEditors', rowIndex);
        if (editors.length == 0)
            return;
        var jeEditor = editors[1]; // 物资编码编辑框
//        if (editors.length == 5)
//            sbslEditor = editors[4];
//        else
//            sbslEditor = editors[6];
        //绑定事件
        //计算总量
        jeEditor.target.unbind("change").bind("change", function () {
            var temp = parseFloat($(this).val()).toFixed(2);
            var sum = 0;
            var data = $(grid).edatagrid("getData");
            for (var i = 0; i < data.rows.length; i++) {
                if (i == rowIndex)
                    sum += parseFloat(temp);
                else
                    sum += parseFloat(data.rows[i]['fkje']);
            }
            var type = $("#zjjhglForm [name='type']:checked").val();
            var footer = null;
            if (type == 'Zgzjjh')
                footer = {"fkrq": "合计", "fkje": sum.toFixed(2)};
            if (type == 'Tpzjjh')
                footer = {"fkrq": "合计", "fkje": sum.toFixed(2)};
            $(grid).datagrid('reloadFooter', [footer]);
        });
    }

    //计算计划总量
    function countSum() {
        var sum = 0;
        var data = $(grid).edatagrid("getData");
        for (var i = 0; i < data.rows.length; i++) {
            sum += parseFloat(data.rows[i]['fkje']);
        }
        var type = $("#zjjhglForm [name='type']:checked").val();
        var footer = null;
        if (type == 'Zgzjjh')
            footer = {"fkrq": "合计", "fkje": sum.toFixed(2)};
        if (type == 'Tpzjjh')
            footer = {"fkrq": "合计", "fkje": sum.toFixed(2)};
        $(grid).datagrid('reloadFooter', [footer]);
    }
});